package com.example.orderedbroadcast;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    MyBroadcastReceiverOne receiverOne;
    MyBroadcastReceiverTwo receiverTwo;
    MyBroadcastReceiverThree receiverThree;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerReceiver();
        init();
    }

    private void  registerReceiver(){
        //动态注册MyBoradcastReceiverTwo广播
        receiverTwo=new MyBroadcastReceiverTwo();
        IntentFilter filter2=new IntentFilter();
        filter2.setPriority(1000);
        filter2.addAction("Intercept_Stitch");
        registerReceiver(receiverTwo,filter2);
        //动态注册MyBoradcastReceiverOne广播
        receiverOne=new MyBroadcastReceiverOne();
        IntentFilter filter1=new IntentFilter();
        filter1.setPriority(1000);
        filter1.addAction("Intercept_Stitch");
        registerReceiver(receiverOne,filter1);

        //动态注册MyBoradcastReceiverThree广播
        receiverThree=new MyBroadcastReceiverThree();
        IntentFilter filter3=new IntentFilter();
        filter3.setPriority(600);
        filter3.addAction("Intercept_Stitch");
        registerReceiver(receiverThree,filter3);
    }


    private void init() {
        Button btn_send=(Button) findViewById(R.id.btn_send);
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("MyBroadcastReceiver",   "onClick: button clicked!");
                Intent intent=new Intent();
                intent.setAction("Intercept_Stitch"); //intercept-拦截，截断，窃听
                sendOrderedBroadcast(intent,null);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiverOne);
        unregisterReceiver(receiverTwo);
        unregisterReceiver(receiverThree);
    }
}
